package cn.christ.com.mobilesafe.view.activity_sjfd;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.christ.com.mobilesafe.R;
import cn.christ.com.mobilesafe.common.BaseActivity;
import cn.christ.com.mobilesafe.common.Global;
import cn.christ.com.mobilesafe.common.utils.Contactsutils.ContactInfo;
import cn.christ.com.mobilesafe.common.utils.Contactsutils.ContactsUtils;
import cn.christ.com.mobilesafe.constant.Constants;

/**
 * Created by Administrator on 2018/7/22.
 */

public class SelectContactsActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_select_contacts)
    ListView lvSelectContacts;
    @BindView(R.id.ll_loading)
    LinearLayout llLoading;
    private List<ContactInfo> contactInfos;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_select_contacts;
    }

    @Override
    public void initView() {
        Global.setStatusBarPadding(tvTitle);
    }

    @Override
    public void initListener() {
        lvSelectContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ContactInfo contactInfo = contactInfos.get(position);
                Intent intent = new Intent();
                intent.putExtra(Constants.PHONE, contactInfo.phone);
                setResult(Constants.RESP_SELECT_CONTACT_CODE, intent);
                SelectContactsActivity.this.finish();
            }
        });
    }

    @Override
    public void initData() {
        AndPermission.with(getApplicationContext())
                .requestCode(Constants.READ_CONTACTS)
                .permission(Manifest.permission.READ_CONTACTS)
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        if (Constants.READ_CONTACTS == requestCode) {
                            getContactsData();
                        }
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        if (AndPermission.hasAlwaysDeniedPermission(SelectContactsActivity.this, deniedPermissions)) {
                            AndPermission.defaultSettingDialog(SelectContactsActivity.this, Constants.READ_CONTACTS).show();
                        }
                    }
                })
                .start();


    }

    /**
     * 获取联系人数据
     */
    private void getContactsData() {
        llLoading.setVisibility(View.VISIBLE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                contactInfos = ContactsUtils.queryAllContacts(getApplicationContext());
                SystemClock.sleep(1000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        lvSelectContacts.setAdapter(new ContactsAdapter());
                        llLoading.setVisibility(View.GONE);
                    }
                });
            }
        }).start();
    }

    @Override
    public void initPresenter() {

    }

    private class ContactsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return contactInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = Global.inflate(R.layout.view_lv_select_safenum);
                ButterKnife.bind(this, convertView);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            ContactInfo contactInfo = contactInfos.get(position);
            viewHolder.tvContactName.setText(contactInfo.name);
            viewHolder.tvContactPhone.setText(contactInfo.phone);
            Bitmap bitmap = ContactsUtils.getBitmapFromContactId(getApplicationContext(), contactInfo.contactId);
            if (bitmap != null) {
                viewHolder.ivContact.setImageBitmap(bitmap);
            } else {
                viewHolder.ivContact.setImageResource(R.drawable.ic_launcher);
            }
            return convertView;
        }

    }

    public class ViewHolder {
        @BindView(R.id.iv_contact)
        ImageView ivContact;
        @BindView(R.id.tv_contact_name)
        TextView tvContactName;
        @BindView(R.id.tv_contact_phone)
        TextView tvContactPhone;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
